//
//  EmotionKeyBoardView.swift
//  1120自定义键盘
//
//  Created by 江先铨 on 15/11/20.
//  Copyright © 2015年 jxqstatic. All rights reserved.
//

import UIKit

private let cellID = "cell"
class EmotionKeyBoardView: UIView {
    
    //声明一个闭包属性
    var selectEmotion: ((em : Emotion) -> ())?
    
    //返回的时总共有多少组 数据  还有一个是返回有多少行数据
    private lazy var packages = EmotionManager().packages
    
    
    //当点击按钮的时候滚动到那一行
    @objc private func itemClick(item: UIBarButtonItem) {
    
         print(item.tag)
        //返回有多少组  每一组有21
          let indexPath = NSIndexPath(forItem: 0, inSection: item.tag)
        //让图片滚动到最左边
          collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: true)
    }
    
    init(selectEmotion: (em : Emotion) -> ()) {
    
       //将外部传递的闭包使用属性 记录
        self.selectEmotion = selectEmotion
        
        let rect = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 220)
        
        super.init(frame: rect)
        
         setupUI()
    }

//    init() {
//        let rect = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 220)
//        super.init(frame: rect)
//        
//        //self.backgroundColor = UIColor.redColor()
//        setupUI()
//
//    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
         addSubview(toolBar)
         addSubview(collectionView)
        
        toolBar.snp_makeConstraints { (make) -> Void in
            make.bottom.left.right.equalTo(self)
            //设置高度
            make.height.equalTo(36)
        }
        
        collectionView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }
        
        
        setuptoolBar()
    }
    
    //懒加载所有的控件
    private lazy var toolBar: UIToolbar = UIToolbar()
    private lazy var collectionView : UICollectionView = {
        
      let layout = UICollectionViewFlowLayout()
        //间隔 每一个元素与下一个元素左右之间的距离
       layout.minimumInteritemSpacing = 0
        //行间距 每一个元素与下一个元素上下之间的距离
        layout.minimumLineSpacing = 0
        
        let w = UIScreen.mainScreen().bounds.width / 7
        layout.itemSize = CGSize(width: w , height: w)
       layout.scrollDirection = .Horizontal
        let margin = (self.bounds.height - 3 * w - 36) / 4
        //这个是设置组于组之间的间距  组间距
       layout.sectionInset = UIEdgeInsets(top: margin, left: 0, bottom: margin, right: 0)
        
      let cv = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
      cv.pagingEnabled = true
        //注册一个collection
      cv.registerClass(EmotionViewCell.self , forCellWithReuseIdentifier: cellID)
        cv.dataSource = self
        cv.delegate = self

        return cv
    }()
    
    private func  setuptoolBar() {
        
        //设置 toolbar 文本渲染颜色
        toolBar.tintColor = UIColor.lightGrayColor()
        
      //声明一个数组
      var items = [UIBarButtonItem]()
        
        var index = 0
        for title in packages {
         //遍历下面的按钮
          let item = UIBarButtonItem(title : title.group_name_cn, style: .Plain, target: self, action: "itemClick:")
          items.append(item)
            
            items.last?.tag = index++
          
            //  实例化一个弹簧
          let space = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
          items.append(space)
        }
        
        items.removeLast()
        
        toolBar.items = items
    }
}


//专门给当前视图扩充数据源方法
extension EmotionKeyBoardView: UICollectionViewDataSource ,UICollectionViewDelegate {
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return packages.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
         return packages[section].emotionList.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellID, forIndexPath: indexPath) as! EmotionViewCell
        //设置背景颜色为什么颜色的
      cell.backgroundColor = indexPath.row % 2 == 0 ? UIColor.redColor() : UIColor.cyanColor()
      cell.emotionIcon = packages[indexPath.section].emotionList[indexPath.item]
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        let em = packages[indexPath.section].emotionList[indexPath.item]
        //当点击的时候给属性赋值
        selectEmotion?(em: em)
    }
}







